package com.sky.service.impl;

import com.sky.dto.DishDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFalvorMapper;
import com.sky.mapper.DishMapper;
import com.sky.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFalvorMapper dishFalvorMapper ;
    /**
     *
     * 新增菜品和对应的口味吧
      * @param dishDTO
     */
    @Transient
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish  dish = new Dish();

        BeanUtils.copyProperties(dishDTO, dish);
     //向菜品表加入数据（加入1条）
      dishMapper.insert(dish);
      //获取insert访问的主键值
        Long id=dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null&&flavors.size()>0) {

            flavors.forEach(dishFlavor -> {
               dishFlavor.setDishId(dish.getId());
            });
            //向口味表加入多条数据
            dishFalvorMapper.insertBatch(flavors);
            }

    }
}
